﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BotonJXC.Entity;
using NHibernate;
using BotonJXC.Util;
using NHibernate.Criterion;

namespace BotonJXC.DAO
{
    public class InStocksDao : NHibernateDao<InStocks, int>
    {



        /// <summary>
        /// 根据入库单编号查询入库单
        /// </summary>
        /// <param name="orderSn"></param>
        /// <returns></returns>
        public IList<InStocks> findInStocksBySn(string InSn,string CreatePlace)
        {
            List<ICriterion> criterions = new List<ICriterion>();
            if (Utils.IsNotBlank(InSn))
            {
                criterions.Add(Restrictions.Eq("InSn", InSn));
            }
            if (Utils.IsNotBlank(CreatePlace))
            {
                criterions.Add(Restrictions.Eq("CreatePlace", CreatePlace));
            }
            return base.Find(criterions.ToArray());
        }


         
        public void SaveInStocks(InStocks InStocks)
        {
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {

                base.Save(InStocks);
                trans.Commit();
            }
            catch (HibernateException ex)
            {
                trans.Rollback();
                throw;

            }
            finally
            {
                HBHelper.Close();
            }

        }

        public void DeleteInStocks(InStocks InStocks)
        {
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {

                base.Delete(InStocks);
                trans.Commit();
            }
            catch (HibernateException ex)
            {
                trans.Rollback();
                throw;

            }
            finally
            {
                HBHelper.Close();
            }

        }
        public PageModel<InStocks> FindPage(InStocks InStocks, DateTime begintime, DateTime endtime, int start, int count)
        {
            List<ICriterion> criterions = new List<ICriterion>();
             
            if (begintime != null)
            {
                criterions.Add(Restrictions.Ge("CreateTime", begintime));
            }
            if (endtime != null)
            {
                criterions.Add(Restrictions.Le("CreateTime", endtime));
            }
            if ( InStocks.OrgId > 0 )
            {
                criterions.Add(Restrictions.Eq("OrgId", InStocks.OrgId));
            }
            if (InStocks.OrgDepId > 0)
            {
                criterions.Add(Restrictions.Eq("OrgId", InStocks.OrgDepId));
            }
            if ( Utils.IsNotBlank( InStocks.InSn ))
            {
                criterions.Add(Restrictions.Eq("OrgId", InStocks.InSn));
            }
            if ( Utils.IsNotBlank( InStocks.AuditStatus))
            {
                criterions.Add(Restrictions.Eq("OrgId", InStocks.AuditStatus));
            }
            

            Order order = new Order("CreateTime", false);

            return base.FindByPage(criterions.ToArray(), order, start, count);
        }

        public PageModel<InStocks> FindPage(InStocks InStocks , int start, int count)
        {
            List<ICriterion> criterions = new List<ICriterion>();


            if (Utils.IsNotBlank(InStocks.InType))
            {
                criterions.Add(Restrictions.Eq("InType", InStocks.InType));
            }
            if (Utils.IsNotBlank(InStocks.CreatePlace))
            {
                criterions.Add(Restrictions.Eq("CreatePlace", InStocks.CreatePlace));
            }
 
            Order order = new Order("CreateTime", false);

            return base.FindByPage(criterions.ToArray(), order, start, count);
        }
    }
}
